<?php
namespace Admin\Controller;
use Think\Controller;
class ProattrController extends CommonController 
{	
	public $product ;

	public $pro_cate ;

	public $color ;

	public $size ;

	public $model ;

	public $pro_attr ; 

	public function _initialize()
	{
		$this->product = M('product');

		$this->pro_cate = M('pro_cate');

		$this->color = M('color');

		$this->size = M('size');

		$this->model = M('model');

		$this->pro_attr = M('pro_attr');

	}


	public function showlist()
	{
		//$data = M('user  as  a')->join('jifen  as  b  on b.id = a.id')->where('a.id  =  1')->select();
		

		$products = M('product as a');

		$field = 'a.pro_id,a.pro_name,a.create_time,c.catename';

		
		if(IS_POST){

			$search = I('post.search');

			$list = $products->field($field)->join('rl_pro_cate as b on a.pro_id = b.pro_id')->join('rl_category as c on c.cate_id = b.cate_id')->where(array('a.pro_name'=>array('like',"%$search%")))->select();

			foreach($list as $k => $v){
				if($this->pro_attr->where(array('pro_id'=>$v['pro_id']))->find()){
					$list[$k]['status'] = 1;
				}else{
					$list[$k]['status'] = 0;
				}
				$list[$k]['create_time'] = date('Y-m-d',$v['create_time']);
			}
			$this->assign('list',$list);
			$this->ajaxReturn($list);

		}else{
			$list = $products->field($field)->join('rl_pro_cate as b on a.pro_id = b.pro_id')->join('rl_category as c on c.cate_id = b.cate_id')->page($_GET['p'],5)->order('create_time')->select();//
			foreach($list as $k => $v){
				if($pro_attr = $this->pro_attr->where(array('pro_id'=>$v['pro_id']))->find()){
					$list[$k]['status'] = 1;
				}else{
					$list[$k]['status'] = 0;
				}
				$list[$k]['create_time'] = date('Y-m-d',$v['create_time']);
				$list[$k]['attr_id'] = $pro_attr['attr_id'];
			}
			$count = $products->count();
			$page = new \Think\Page($count,5);
			$show = $page->show();

			
			$this->assign('page',$show);
			$this->assign('list',$list);
			
			$this->display();
		}

		

		
		
	}

	public function append()
	{
		$pro_id = I('get.pro_id');
		
		
		$pro_inventory = M('pro_inventory');

		$inventory = $pro_inventory->where(array('pro_id'=>$pro_id))->select();
		$color = $this->color->select();
		$product = $this->product->where(array('pro_id'=>$pro_id))->find();
		$size = $this->size->select();
		$this->assign('inventory',$inventory);
		$this->assign('color',$color);
		$this->assign('product',$product);
		$this->assign('size',$size);
		$this->assign('pro_id',$pro_id);

		if(IS_AJAX){

			$data = I('post.');
			
			// var_dump($data);die;
			//状态码1,添加库存
			if($data['status']==1){
				if(!$pro_inventory->create()){

				}else{
					if($pro_inventory->where(array('pro_id'=>$data['pro_id'],'color_id'=>$data['color_id'],'size_id'=>$data['size_id']))->find()){
						$info['code'] = '0';
						$info['info'] = '已存在相同规格商品';
						$this->ajaxReturn($info);
					}else{
						if($pro_inventory->add($data)){
							$info['code'] = '1';
							$info['info'] = '添加成功';
							$this->ajaxReturn($info);
						}else{
							$info['code'] = '0';
							$info['info'] = '添加失败';
							$this->ajaxReturn($info);
						}
					}
				}
			}elseif($data['status']==2){
			//状态码2,修改库存
				// var_dump($data);die;
				if(!$pro_inventory->create()){

				}else{
					if($pro_inventory->where(array('pro_id'=>$data['pro_id'],'color_id'=>$data['color_id'],'size_id'=>$data['size_id'],'pr_co_id'=>array('neq',$data['pr_co_id'])))->find()){
						$info['code'] = '0';
						$info['info'] = '已存在相同规格商品';
						$this->ajaxReturn($info);
					}else{
						if(!$pro_inventory->where(array('pr_co_id'=>$data['pr_co_id']))->save($data)){
							$info['code'] = '0';
							$info['info'] = '修改失败';
							$this->ajaxReturn($info);
						}else{
							
							$info['code'] = '1';
							$info['info'] = '修改成功';
							$this->ajaxReturn($info);
							
						}
					}
				}
			}elseif($data['status']==3){
				//状态码3,删除库存
				if(!$pro_inventory->where(array('pr_co_id'=>$data['pr_co_id']))->delete()){
					$info['code'] = '0';
					$info['info'] = '删除失败';
					$this->ajaxReturn($info);
				}else{
					$info['code'] = '1';
					$info['info'] = '删除成功';
					$this->ajaxReturn($info);
							
				}
			}

		}else{
			$this->display();
		}
		

	}

	public function edit()
	{	


		$pro_id = I('get.pro_id');//获取当前修改的PRO_ID
		// var_dump($pro_id) ;die;		

		$product = $this->product->join('rl_pro_cate on rl_pro_cate.pro_id = rl_product.pro_id')->join('rl_category on rl_category.cate_id = rl_pro_cate.cate_id')->where(array('rl_product.pro_id'=>$pro_id))->find(); //原数据查找
		
		
		$model = $this->model->select();//模特列表

		$pro_attr = $this->pro_attr->where(array('pro_id'=>$pro_id))->find();//当前商品属性属性
		
		// var_dump($pro_attr);die;
		$this->assign('product',$product);//原数据
		$this->assign('model_list',$model);
		$this->assign('pro_attr',$pro_attr);

		

		// foreach($exist_color as $v) {
		// 	$exist_color_id[] = $v['color_id'];
		// }
		// $this->assign('exist_color',$exist_color);// 已存在的颜色
		
		if(IS_POST){
			// $data = ('main_img');

			$data = I('post.');
			//图片上传
			if(!empty($_FILES)){
				if($data['water_type']==0){
					if($this->pro_attr->create()){

						$result = $this->get_upload();//文件上传
						// var_dump($result);die;
						if(!empty($result)){
							//上传成功后
							$thumb_path = $this->get_thumb($result[0][0],210,263);

							if(!empty($thumb_path)){
								$data['main_img'] = $result[0][0].','.$thumb_path;
								$data['detail_img'] = implode(',',$result[1]);
								$data['detail_intro_img'] = implode(',',$result[2]);
							}
						}
					}
				}elseif($data['water_type']==1){
					if($this->pro_attr->create()){

						$result = $this->get_upload();//文件上传

						// var_dump($result);die;
						if(!empty($result)){
							//上传成功后
							$thumb_path = $this->get_thumb($result[0][0],210,263);
							foreach($result as $key => $val){
								foreach($val as $ke => $va){
									$result[$key][$ke] = $this->get_picwater($va,$result[3][0]);
								}
							}
							if(!empty($thumb_path)){
								$data['main_img'] = $result[0][0].','.$thumb_path;
								$data['detail_img'] = implode(',',$result[1]);
								$data['detail_intro_img'] = implode(',',$result[2]);
							}
						}
					}
				}elseif($data['water_type']==2){
					if($this->pro_attr->create()){
						
						$result = $this->get_upload();//文件上传
						// var_dump($result);die;
						if(!empty($result)){
							//上传成功后
							$thumb_path = $this->get_thumb($result[0][0],210,263);
							foreach($result as $key => $val){
								foreach($val as $ke => $va){
									$result[$key][$ke] = $this->get_textwater($va,$data['water']);
								}
							}


							if(!empty($thumb_path)){
								$data['main_img'] = $result[0][0].','.$thumb_path;
								$data['detail_img'] = implode(',',$result[1]);
								$data['detail_intro_img'] = implode(',',$result[2]);
							}
						}
					}
				}
			}
			

			
			
			
			// $data = $_FILES['main_img'];
			if($data['restrict_nub']==0){
				$data['restrict_nub'] = 9999;
			}
			
			$data['pro_id'] = $pro_id;
			
			//更新数据
			$data['pro_name'] = I('post.pro_name');
			
			$data['cate_id'] = I('post.category');

			

			if($pro_attr){

				$pr_attr_upd = $this->pro_attr->where(array('attr_id'=>$pro_attr['attr_id']))->save($data);

			}else{

				$pr_attr_upd = $this->pro_attr->add($data);

			}

			

			// var_dump($manager->getLastSql());
			if($pr_attr_upd || $pr_ca_upd){
				$info['code'] = 1;
				$info['info'] = '修改成功';
				$info['status'] = 'success';
				$this->ajaxReturn($info);
			}else{
				$info['code'] = 0;
				$info['info'] = '修改失败';
				$info['status'] = 'error';
				$this->ajaxReturn($info);
			}

		}else{
			$this->display();
		}

		
	}

	public function get_upload()
	{	
		//检查文件夹
		$path = './Public/Admin/upload/img/';
		//文件夹不存在则创建文件夹
		if(!file_exists($path)){
			mkdir($path,0777,true);
			$path = rtrim($path,'/').'/';
		}

		$config = array(

			//限制文件大小
			'maxSize'	=>	3145728,
			//设置保存目录
			'rootPath'	=>	$path,
			//上传文件的保存名称
			'saveName'	=>	array('uniqid',''),
			//文件上传的格式类型
			'exts'		=>	array('jpg','gif','png','jpeg'),
			'autoSub'	=>	ture,
			'subName'	=>	array('date','Ymd'),
		);
		$upload = new \Think\Upload($config);//实例化上传类
		//文件上传,支持多文件上传
		
		$info[0] = $upload->upload(array('main_img'=>$_FILES['main_img']));
		$info[1] = $upload->upload(array('detail_img'=>$_FILES['detail_img']));
		$info[2] = $upload->upload(array('detail_info_img'=>$_FILES['detail_info_img']));
		if(!empty($_FILES['water'])){
			$info[3] = $upload->upload(array('water'=>$_FILES['water']));
		}
		$result = array();
		if(!$info){
			return $result;
		}else{
			foreach($info as $key => $val){
				foreach($val as $va){
					$result[$key][] = $path.$va['savepath'].$va['savename'];
				}
			}
			return $result;
		}
	}
	//缩略
	public function get_thumb($path,$width,$hieght){

		$thumb_path = substr($path,0,strrpos($path,'/')+1).$width.'x'.$hieght.'_'.substr($path,strrpos($path,'/')+1);

		$image = new \Think\Image();
		$image->open($path);
		$image->thumb($width,$hieght)->save($thumb_path);
		return $thumb_path;
	}
	//文字水印
	public function get_textWater($path,$water){
		$image = new \Think\Image();
		$image->open($path)->text($water,'Public/Admin/ttf/msyh.ttf',20,'#000000',\Think\Image::IMAGE_WATER_SOUTHEAST)->save($path);
		return $path;
	}
	//图片水印
	public function get_picwater($path,$water,$position = 9){
		$image = new \Think\Image();
		$image->open($path)->water($water,$position)->save($path);
		return $path;
	}

	public function del()
	{	
		

		if(IS_AJAX){

			$data['attr_id'] = I('post.attr_id');
			
			

			$del = $this->pro_attr->where(array('attr_id'=>$data['attr_id']))->delete();


			if($del){
				$info['code'] = 1;
				$info['info'] = '删除成功';
				$info['status'] = 'success';
				$this->ajaxReturn($info);
			}else{
				$info['code'] = 0;
				$info['info'] = '删除失败';
				$info['status'] ='error';
				$this->ajaxReturn($info);
			}

		}
	}

	
}

?>